package iso3.pt.action;

import iso3.pt.dao.IncorrectPasswordException;
import iso3.pt.dao.PtDAO;
import iso3.pt.dao.UserNotFoundException;
import iso3.pt.model.Alumno;
import iso3.pt.model.Asignatura;
import iso3.pt.model.Profesor;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;



public class LoginAction extends ActionSupport implements Preparable {
	private Alumno alum;
	private Profesor prof;
	private String username;
	private String password;
	private PtDAO dao;
	private String tipoLogin;
	private Set<Asignatura> listaAsignaturas;
	
	
	
	public Set<Asignatura> getListaAsignaturas() {
		return listaAsignaturas;
	}




	public void setListaAsignaturas(Set<Asignatura> listaAsignaturas) {
		this.listaAsignaturas = listaAsignaturas;
	}




	public PtDAO getDao() {
		return dao;
	}




	public void setDao(PtDAO dao) {
		this.dao = dao;
	}




	public String getTipoLogin() {
		return tipoLogin;
	}




	public void setTipoLogin(String tipoLogin) {
		this.tipoLogin = tipoLogin;
	}




	public Alumno getAlum() {
		return alum;
	}




	public void setAlum(Alumno alum) {
		this.alum = alum;
	}




	public Profesor getProf() {
		return prof;
	}




	public void setProf(Profesor prof) {
		this.prof = prof;
	}




	



	public String getUsername() {
		return username;
	}




	public void setUsername(String username) {
		this.username = username;
	}




	public String getPassword() {
		return password;
	}




	public void setPassword(String password) {
		this.password = password;
	}




	@Override
	public void prepare() throws Exception {
		System.out.println("Una buena sysada 1");
		dao= PtDAO.getInstance();
		System.out.println("Una buena sysada 2");
	}
	
	public String execute(){
		if(tipoLogin.equals("prof")){

			try {
				prof= dao.loginProfesor(Integer.parseInt(username), password);
				listaAsignaturas= dao.getAsignaturasProfesor(prof.getId());
				Map<String,Object> session= ActionContext.getContext().getSession();
				session.put("tipoLogin", tipoLogin);
				session.put("username", username);
				session.put("password", password);
				ActionContext.getContext().setSession(session);
			} catch (NumberFormatException e) {
				addActionError(getText("errors.incorrectFormat"));
				System.out.println("Error de formato");
				e.printStackTrace();
				return ERROR;
			} catch (UserNotFoundException e) {
				addActionError(getText("errors.userNotFound"));
				System.out.println("Error de usuario");
				e.printStackTrace();
				return ERROR;
			} catch (IncorrectPasswordException e) {
				addActionError(getText("errors.incorrectPassword"));
				System.out.println("Error de password");
				e.printStackTrace();
				return ERROR;
			}
			return "profesor";
		}else{
			try {
				alum= dao.loginAlumno(Integer.parseInt(username), password);
				listaAsignaturas= dao.getAsignaturas(Integer.parseInt(username));
				Map<String,Object> session= ActionContext.getContext().getSession();
				session.put("tipoLogin", tipoLogin);
				session.put("alum", alum);
				ActionContext.getContext().setSession(session);
			} catch (NumberFormatException e) {
				addActionError(getText("errors.incorrectFormat"));
				System.out.println("Error de formato");
				e.printStackTrace();
				return ERROR;
			} catch (UserNotFoundException e) {
				addActionError(getText("errors.userNotFound"));
				System.out.println("Error de usuario");
				e.printStackTrace();
				return ERROR;
			} catch (IncorrectPasswordException e) {
				addActionError(getText("errors.incorrectPassword"));
				System.out.println("Error de password");
				e.printStackTrace();
				return ERROR;
			}
			return "alumno";
		}
	}

}
